package com.flavors.wan.main.vm;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.flavors.wan.main.bean.BannerBean;
import com.flavors.wan.main.bean.CoinBean;
import com.flavors.wan.main.bean.UserBean;
import com.flavors.wan.main.model.UserUtils;
import com.flavors.wan.main.net.BaseApi;
import com.flavors.wan.main.net.RequestManager;
import com.lzy.okgo.model.Response;
import com.plat.sdk.okgo.bean.ResponseBean;
import com.plat.sdk.okgo.callbck.JsonCallback;
import com.plat.sdk.okgo.callbck.XmResource;
import com.plat.sdk.okgo.exception.MyException;

import java.util.List;


/**
 * @author Created by ydong on 2020/1/7 18:36
 * @desc: 处理具体的网络请求过程，然后通过回调把结果传递到VM中，VM再通过setValue来将数据回传给UI
 */
public class WanVM extends AndroidViewModel {


    private final MutableLiveData<XmResource<CoinBean>> coinbean = new MutableLiveData<>();
    private final MutableLiveData<XmResource<List<BannerBean>>> banners = new MutableLiveData<>();
    public MutableLiveData<XmResource<CoinBean>> getCoinBean() {
        return coinbean;
    }

    public MutableLiveData<XmResource<List<BannerBean>>> getBanners() {
        return banners;
    }
    public WanVM(@NonNull Application application) {
        super(application);
    }

    private final MutableLiveData<XmResource<UserBean>> loginData = new MutableLiveData<>();

    public MutableLiveData<XmResource<UserBean>> getLoginData() {
        return loginData;
    }


    public void fetchLogin(String username, String password) {
        RequestManager.getInstance().loginClick(BaseApi.LOGIN, this, username, password, new JsonCallback<ResponseBean<UserBean>>() {
            @Override
            public void onSuccess(Response<ResponseBean<UserBean>> response) {
                ResponseBean<UserBean> body = response.body();
                UserUtils.getInstance().login(body.getData());
                getLoginData().setValue(XmResource.success(body.getData()));//需要增加包装类，不然UI无法区分类型
            }

            @Override
            public void onError(Response<ResponseBean<UserBean>> response) {
                super.onError(response);
                ResponseBean<UserBean> body = response.body();
                getLoginData().setValue(XmResource.error(body.errorCode, body.errorMsg));
            }
        });
    }


    public void fetchCoin() {
        RequestManager.getInstance().getCoinInfo(BaseApi.USER_COIN, this, null, new JsonCallback<ResponseBean<CoinBean>>() {
            @Override
            public void onSuccess(Response<ResponseBean<CoinBean>> response) {
                ResponseBean<CoinBean> body = response.body();
                getCoinBean().setValue(XmResource.success(body.getData()));
            }

            @Override
            public void onError(Response<ResponseBean<CoinBean>> response) {
                super.onError(response);
                if (response.getException() instanceof MyException) { //个人自定义 异常 根据后台 约定值判断异常雷系
                    if (((MyException) response.getException()).getErrorBean().errorCode == -1001) {//约定的身份表示过期,重登录
                        getCoinBean().setValue(XmResource.error(-1001, "约定的身份表示过期,请重新登录"));
                        Log.e("ydong", "-1001 约定的身份表示过期");
                    }
                }
            }
        });
    }

    /**
     * BANNER
     */
    public void fetchBanner() {
        getBanners().setValue(XmResource.loading());
        RequestManager.getInstance().getBannerList(BaseApi.BANNER, this, null, new JsonCallback<ResponseBean<List<BannerBean>>>() {
            @Override
            public void onSuccess(Response<ResponseBean<List<BannerBean>>> response) {
                ResponseBean<List<BannerBean>> body = response.body();
                getBanners().setValue(XmResource.success(body.data));
            }

            @Override
            public void onError(Response<ResponseBean<List<BannerBean>>> response) {
                super.onError(response);
                ResponseBean<List<BannerBean>> body = response.body();
                getBanners().setValue(XmResource.error(body.errorCode, body.errorMsg));
            }
        });
    }
}
